package com.reader.cms.service.impl.sys;

import com.baomidou.mybatisplus.plugins.Page;
import com.reader.cms.entity.sys.SysMenu;
import com.reader.cms.entity.sys.SysToken;
import com.reader.cms.entity.sys.SysUser;
import com.reader.cms.mapper.sys.SysUserMapper;
import com.reader.cms.service.sys.ISysTokenService;
import com.reader.cms.service.sys.ISysUserService;
import com.reader.cms.service.sys.ISysMenuService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.reader.core.utils.CommonVariable;
import com.reader.core.utils.EncryptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户实现类
 * </p>
 *
 * @author jiahaikun
 * @date 2018-07-26
 */
@Service("iSysUserService")
@Transactional
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private ISysMenuService iSysMenuService;

    @Autowired
    private ISysTokenService iSysTokenService;

    @Override
    public SysUser getUserInfo(SysUser user) {
        SysUser userInfo = sysUserMapper.getUserInfo(user);
        List<String> userRolesId = sysUserMapper.getUserRolesId(userInfo);
        List<String> userRolesName = sysUserMapper.getUserRolesName(userInfo);
        List<Map<String, String>> userPermissions = null;
        List<SysMenu> userMenus = null;
        if (userRolesId.contains(CommonVariable.SUPER_ADMIN_ROLE)) {
            userPermissions = sysUserMapper.getAllPermissions();
            userMenus = iSysMenuService.getAllMenus();
        } else {
            userPermissions = sysUserMapper.getUserPermissions(userInfo);
            userMenus = iSysMenuService.getUserMenus(userInfo);
        }

        userInfo.setRolesId(userRolesId);
        userInfo.setRolesName(userRolesName);
        userInfo.setPermissions(userPermissions);
        userInfo.setMenus(userMenus);

        return userInfo;
    }

    @Override
    public Page<Map<String, Object>> selectListPage(int currentPage, int size, String loginId, String name, String code) {
        Page<Map<String, Object>> page = new Page<>(currentPage, size);// 当前页，总条数 构造 page 对象
        return page.setRecords(sysUserMapper.selectListPage(page, loginId, name,code));
    }

    @Override
    public void configUserRoles(SysUser sysUser) {
        sysUserMapper.deleteUserRole(sysUser.getId());
        sysUserMapper.insertUserRole(sysUser.getId(),sysUser.getRolesId());
    }

    @Override
    public boolean updateUser(SysUser sysUser) {
        boolean flag = false;
        SysUser user = new SysUser();
        user.setId(sysUser.getId());
        SysUser u = sysUserMapper.selectOne(user);
        if(!u.getPassword().equals(sysUser.getPassword())) {
            String sha256Password= EncryptUtil.encryptPassword(
                    CommonVariable.ENCRYTP_ALGORITHMNAME,
                    sysUser.getPassword(),
                    CommonVariable.ENCRYTP_SALT,
                    CommonVariable.ENCRYTP_ITERATIONS);
            sysUser.setPassword(sha256Password);
        }
        if(!u.getOrganizationId().equals(sysUser.getOrganizationId())){
            sysUserMapper.deleteUserRole(sysUser.getId());
            sysUser.setRolesName(new ArrayList<>());
        }
        Integer integer = sysUserMapper.updateById(sysUser);
        if(integer == 1){
            flag = true;
        }
        return flag;
    }

    @Override
    public boolean deleteUser(SysUser sysUser) {
        boolean flag = false;
        sysUserMapper.deleteUserRole(sysUser.getId());
        Integer integer = sysUserMapper.updateById(sysUser);
        if(integer == 1){
            flag = true;
        }
        return false;
    }

    @Override
    public Integer getUserIdByToken(String token) {
        SysToken tokenInfo=iSysTokenService.getTokenInfoByToken(token);
        if(tokenInfo==null||tokenInfo.getUserID()==null){
            return null; //查不到userID
        }else {
            return tokenInfo.getUserID();
        }
    }

    @Override
    public String getLoginIdByToken(String token) {
        SysToken tokenInfo=iSysTokenService.getTokenInfoByToken(token);
        if(tokenInfo==null||tokenInfo.getLoginId()==null){
            return null; //查不到LoginId()
        }else {
            return tokenInfo.getLoginId();
        }
    }
}
